package com.smartcommunity.service.dataImport;

import com.smartcommunity.base.bean.ExceptionCode;
import com.smartcommunity.base.common.PicImport;
import com.smartcommunity.bean.constant.Constant;
import com.smartcommunity.es.common.EsBasedataEnum;
import com.smartcommunity.dao.people.PeopleDao;
import com.smartcommunity.elasticsearch.people.PeopleEsApi;
import com.smartcommunity.exception.BusinessException;
import com.smartcommunity.log.interceptor.Log;
import com.smartcommunity.service.people.PeopleService;
import com.smartcommunity.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.*;

/**
 * Copyright: Copyright (c) 2018
 * Company:
 *
 * @author heweiwei
 * @description 人员证件照导入模块
 * @date 2018/9/3
 */
@Component("peoplePic")
@Slf4j
public class PeoplePicImport implements PicImport {

    private final PeopleDao peopleDao;

    @Autowired
    public PeoplePicImport(PeopleDao peopleDao) {
        this.peopleDao = peopleDao;
    }

    @Autowired
    private PeopleService peopleService;
    @Autowired
    private PeopleEsApi peopleEsApi;

    /**
     * key taskId,value progress
     */
    final int cacheSize = 100000;
    Map<String, Integer> progressTag = new LinkedHashMap<String, Integer>((int) Math.ceil(cacheSize / 0.75f) + 1, 0.75f, true) {
        @Override
        protected boolean removeEldestEntry(Map.Entry<String, Integer> eldest) {
            return size() > cacheSize;
        }
    };

    @Override
    public int getImportPicProgress(String taskId) {
        return progressTag.get(taskId) == null ? 0 : progressTag.get(taskId);
    }

    /**
     * 导入人员证件照
     * @param pathList 图片地址集合，Map中包括小区编码villageCode，照片的标识idNo以及在PFS中保存的地址picPath
     */
    @Override
    @Log(info = "人员照片导入")
    public void importPic(List<Map<String, Object>> pathList,String taskId) throws IllegalArgumentException, BusinessException {
        // 更新证件照
        try {
            progressTag.put(taskId, 20);
            peopleDao.updatePic(pathList);
            progressTag.put(taskId, 50);
            //更新ES
            Map<String, Map<String, Object>> esUpdateMap = new HashMap<String, Map<String,Object>>();
            List<String> idcardList = new ArrayList<>();
            for (Map<String,Object> map:pathList) {
                String idcardNo = map.get("idNo").toString();
                if(StringUtils.isNotEmpty(idcardNo)){
                    idcardList.add(idcardNo);
                    Map<String, Object> valueMap = new HashMap<>();
                    valueMap.put("idCardPic", map.get("picPath").toString());
                    valueMap.put("featureId", 0);
                    esUpdateMap.put(idcardNo+Constant.SEPARATOR+ EsBasedataEnum.PEOPLE.getIntValue(), valueMap);
                }
            }
        	if(!org.springframework.util.CollectionUtils.isEmpty(esUpdateMap)) {
        		peopleEsApi.batchUpdate(esUpdateMap);
        	}
            progressTag.put(taskId, 70);
            peopleService.updateVidAndFeatureIdByEngine(idcardList);
            progressTag.put(taskId, 100);
        } catch (BusinessException e) {
            log.error("更新ES中人员证件照信息失败！", e);
            progressTag.put(taskId, 100);
            throw new BusinessException(ExceptionCode.PEOPLE_PIC_IMPORT_EXCEPTION_CODE, "实有人员证件照导入失败，入库ES异常！");
        } catch (Exception e) {
            log.error("更新人员证件照信息失败！", e);
            progressTag.put(taskId, 100);
            throw new BusinessException(ExceptionCode.PEOPLE_PIC_IMPORT_EXCEPTION_CODE,"实有人员证件照导入失败！");
        }
    }
}
